package com.tongcao.cn.contoller;

import com.tongcao.cn.annotation.SysLog;
import com.tongcao.cn.dfire.result.Result;
import com.tongcao.cn.dfire.result.ResultMap;
import com.tongcao.cn.dfire.utils.ApiResultUtil;
import com.tongcao.cn.oauth.cons.CodeEnum;
import com.tongcao.cn.oauth.request.BaiduJumpRequest;
import com.tongcao.cn.oauth.request.JumpRequest;
import com.tongcao.cn.oauth.request.TencentJumpRequest;
import com.tongcao.cn.oauth.service.IJumpService;
import com.tongcao.cn.oauth.vo.PersonVO;
import com.tongcao.cn.util.ImageUtil;
import com.tongcao.cn.util.QrCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author:tongcao
 * @since:2019/11/22 15:59
 * Description:
 */
@Controller
@RequestMapping("/jump")
public class JumpController {

    @Resource
    private IJumpService jumpService;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap jump(String code) {
        BaiduJumpRequest request = new BaiduJumpRequest(code);
        final Result<PersonVO> result = jumpService.execute(request);
        final PersonVO personVO = result.getModel();
        return ApiResultUtil.successResult(personVO);
    }

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public void redirect(String code, HttpServletResponse response) {
        try {
            JumpRequest request = null;
            if (StringUtils.equals(CodeEnum.BAIDU.getCode(), code)) {
                request = new BaiduJumpRequest(code);
            } else if (StringUtils.equals(CodeEnum.TENCENT.getCode(), code)) {
                request = new TencentJumpRequest(code);
            }
            final Result<PersonVO> result = jumpService.execute(request);
            final PersonVO personVO = result.getModel();
            response.sendRedirect(personVO.getBaseInfoUrl());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试将二维码输出到页面
     *
     * @param request
     * @param response
     */
    @SysLog("生成二维码")
    @RequestMapping("/qrcode")
    public void testQRCode(String code, HttpServletRequest request, HttpServletResponse response) {
        try {
            BaiduJumpRequest req = new BaiduJumpRequest(code);
            req.setCode(code);
            final Result<PersonVO> result = jumpService.execute(req);
            final PersonVO personVO = result.getModel();
            ServletOutputStream outputStream = response.getOutputStream();
            QrCodeUtil.generate(personVO.getBaseInfoUrl(), 150, 150, ImageUtil.IMAGE_TYPE_JPG, outputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }

}